package com.c1202m.dao;

import com.c1202m.gui.BangThongKe;
import com.c1202m.gui.CustomMessageDialog;
import com.c1202m.hibernate.entities.ChuyenKhoan;
import com.c1202m.hibernate.entities.TaiKhoan;
import com.c1202m.util.DateUtil;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import org.hibernate.Query;
import org.hibernate.Session;

public class ChuyenKhoanDAO {

    public static Vector layToanBoDoiTuong() {
        Vector vt = new Vector();
        List listGui = null;
        List listNhan = null;
        Session session = DangNhapDAO.sessionFactory.openSession();
        try {
            session.getTransaction().begin();
            Query queryGui = session.createQuery("select c.ngayCK, g.chuTK, c.soTKGui from ChuyenKhoan c inner join c.taiKhoanGui g");
            Query queryNhan = session.createQuery("select n.chuTK, c.soTKNhan, c.soTienCK from ChuyenKhoan c inner join c.taiKhoanNhan n");
            listGui = queryGui.list();
            listNhan = queryNhan.list();
            Iterator itGui = listGui.iterator();
            Iterator itNhan = listNhan.iterator();
            while (itGui.hasNext() || itNhan.hasNext()) {
                Object[] objGui = (Object[]) itGui.next();
                Object[] objNhan = (Object[]) itNhan.next();
                Vector temp = new Vector();
                temp.add(objGui[0]);
                temp.add(objGui[1]);
                temp.add(objGui[2]);
                temp.add(objNhan[0]);
                temp.add(objNhan[1]);
                temp.add(objNhan[2]);
                vt.add(temp);
            }
            session.getTransaction().commit();
            session.close();
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return vt;
    }

    public static Vector layDoiTuongTheoSoTk(String soTK) {
        Vector vt = new Vector();
        List list = null;
        Session session = DangNhapDAO.sessionFactory.openSession();
        try {
            session.getTransaction().begin();
            Query query = session.createQuery("from ChuyenKhoan a where a.soTKGui = ? or a.soTKNhan = ?");
            query.setString(0, soTK);
            query.setString(1, soTK);
            list = query.list();
            for (Object obj : list) {
                ChuyenKhoan chuyenKhoan = (ChuyenKhoan) obj;
                Vector temp = new Vector();
                temp.add(chuyenKhoan.getNgayCK());
                temp.add(chuyenKhoan.getSoTKGui());
                temp.add(chuyenKhoan.getSoTKNhan());
                temp.add(chuyenKhoan.getSoTienCK());
                vt.add(temp);
            }
            session.getTransaction().commit();
            session.close();
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return vt;
    }

    public static Vector layDoiTuongTheoNgay() {
        Vector vt = new Vector();
        if (BangThongKe.jxDate.getDate() == null) {
            return vt;
        }
        List listGui = null;
        List listNhan = null;
        Session session = DangNhapDAO.sessionFactory.openSession();
        try {
            session.getTransaction().begin();
            Query queryGui = session.createQuery("select c.ngayCK, g.chuTK, c.soTKGui from ChuyenKhoan c inner join c.taiKhoanGui g where c.ngayCK between ? and ?");
            Query queryNhan = session.createQuery("select n.chuTK, c.soTKNhan, c.soTienCK from ChuyenKhoan c inner join c.taiKhoanNhan n where c.ngayCK between ? and ?");

            Date ngayRut = BangThongKe.jxDate.getDate();
            java.sql.Timestamp sqlDateFirstTime = new java.sql.Timestamp(DateUtil.getDayFirstTime(ngayRut).getTime());
            java.sql.Timestamp sqlDateLastTime = new java.sql.Timestamp(DateUtil.getDayLastTime(ngayRut).getTime());
            String strF = String.valueOf(sqlDateFirstTime);
            String strL = String.valueOf(sqlDateLastTime);

            queryGui.setString(0, strF);
            queryGui.setString(1, strL);

            queryNhan.setString(0, strF);
            queryNhan.setString(1, strL);

            listGui = queryGui.list();
            listNhan = queryNhan.list();
            Iterator itGui = listGui.iterator();
            Iterator itNhan = listNhan.iterator();
            while (itGui.hasNext() || itNhan.hasNext()) {
                Object[] objGui = (Object[]) itGui.next();
                Object[] objNhan = (Object[]) itNhan.next();
                Vector temp = new Vector();
                temp.add(objGui[0]);
                temp.add(objGui[1]);
                temp.add(objGui[2]);
                temp.add(objNhan[0]);
                temp.add(objNhan[1]);
                temp.add(objNhan[2]);
                vt.add(temp);
            }
            session.getTransaction().commit();
            session.close();
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return vt;
    }

    public static boolean chuyenKhoan(String soTKGui, String soTKNhan, double soTienChuyen) {
        Session session = DangNhapDAO.sessionFactory.openSession();
        try {
            session.getTransaction().begin();

            TaiKhoan taiKhoanGui = (TaiKhoan) session.get(TaiKhoan.class, soTKGui);
            double soDuTKGui = Double.valueOf(taiKhoanGui.getSoDuTK()) - Double.valueOf(soTienChuyen);
            taiKhoanGui.setSoDuTK(soDuTKGui);
            session.update(taiKhoanGui);

            TaiKhoan taiKhoanNhan = (TaiKhoan) session.get(TaiKhoan.class, soTKNhan);
            double soDuTKNhan = Double.valueOf(taiKhoanNhan.getSoDuTK()) + Double.valueOf(soTienChuyen);
            taiKhoanNhan.setSoDuTK(soDuTKNhan);
            session.update(taiKhoanNhan);

            ChuyenKhoan chuyenKhoan = new ChuyenKhoan();
            chuyenKhoan.setNgayCK(new Date());
            chuyenKhoan.setSoTKGui(soTKGui);
            chuyenKhoan.setSoTKNhan(soTKNhan);
            chuyenKhoan.setSoTienCK(soTienChuyen);
            session.save(chuyenKhoan);
            session.getTransaction().commit();
            session.close();
            return true;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        new CustomMessageDialog(null, true, "Oops...!", "An error occured!", CustomMessageDialog.MESSAGE);
        return false;
    }
}
